package com.example.demo.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.demo.dao.BookMapper;
import com.example.demo.dao.ChapterMapper;
import com.example.demo.model.Book;
import com.example.demo.model.Chapter;
import com.example.demo.model.Tag;
import com.example.demo.model.User;
import com.example.demo.service.TagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * 功能描述：
 *
 * @Author: Zhanghf
 * @Date: 2021/6/22 17:45
 */
@Controller
public class TagController {
    @Autowired
    private TagService tagService;
    @Autowired
    private BookMapper bookMapper;
    @Autowired(required = false)
    private ChapterMapper chapterMapper;

    //    @PostMapping("tag")
    @GetMapping("tag")
    public String totag(HttpServletResponse response, HttpServletRequest request, Model model, HttpSession session) {
        User user = (User) session.getAttribute("user");
        model.addAttribute("user", user);
        List<Tag> tags = tagService.getTag(user.getId());
        List<Chapter> chapters = new ArrayList<>();
        ArrayList<Book> books = new ArrayList<>();
        for (Tag tag : tags) {
            Book book = bookMapper.selectOne(new QueryWrapper<Book>().eq("id", tag.getBookid()));
            books.add(book);
            Chapter chapter = chapterMapper.selectOne(new QueryWrapper<Chapter>().eq("bookid", tag.getBookid()).eq("chapterid", tag.getChapterid()));
          //  Chapters chapter = chaptersService.selectOne(tag.getBookid(), tag.getChapterid());
            chapters.add(chapter);
        }
        model.addAttribute("books", books);
        model.addAttribute("chapters", chapters);
        model.addAttribute("tag", tags);
        model.addAttribute("userid", user.getId());
        return "tag";
    }

    @GetMapping("addtag")
    public String addtag(HttpServletResponse response, HttpServletRequest request, Model model, HttpSession session,
                         @RequestParam("chid") Integer chid, @RequestParam("userid") Integer userid, @RequestParam("bookid") Integer bookid) {
        tagService.addtag(chid, userid, bookid);
        return "forward:text";
    }

    @PostMapping("tagdelete")
    public String historydelete(HttpServletResponse response, HttpServletRequest request, Model model, HttpSession session,
                                @RequestParam("chapterid") Integer chapterid,
                                @RequestParam("userid") Integer userid, @RequestParam("bookid") Integer bookid) {
        {

            tagService.deletetag(chapterid, userid, bookid);
            return "forward:tag";
        }
    }
}